package com.glorysoft.sunnypda.ui.searcheqp

import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.databinding.ObservableArrayList
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.base.Constant
import com.glorysoft.sunnypda.entity.EqpByAreaInfoEntity
import com.glorysoft.sunnypda.entity.ui.CallObserveBean
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.model.EqpRepository
import com.google.gson.JsonObject
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding
import androidx.databinding.BaseObservable
import com.glorysoft.sunnypda.entity.FabEntity
import com.glorysoft.sunnypda.entity.LineEntity

/**
 * FileName: SearchEqpInfoViewModel
 * Author: XJH
 * Date: 2022/1/11 17:56
 * Description:
 * History:
 */
class SearchEqpInfoViewModel : BaseViewModel() {

    val eqpRepository : EqpRepository = EqpRepository()
    val mAreaList : ArrayList<String> = arrayListOf()
    var eqpItems = ObservableArrayList<EqpByAreaInfoEntity>()
    var checkItem = EqpByAreaInfoEntity()
    var fabList = arrayListOf<String>()
    var fabEntityList: ArrayList<FabEntity> = arrayListOf()
    var lineEntityList: ArrayList<LineEntity> = arrayListOf()

    val onClick : EqpInfoClick = object : EqpInfoClick {
        override fun onEqpInfoItemClick(item: EqpByAreaInfoEntity) {
//            for (i in 0..eqpItems.size-1){
//                if (item.EQPID == eqpItems[i].EQPID && !item.isSelector){
//                    eqpItems[i].isSelector = true
//                }else{
//                    eqpItems[i].isSelector = false
//                }
//            }
            item.isSelector = !item.isSelector
//            checkItem = item
//            eqpItems.forEach {
//                it.isSelector = checkItem.EQPID == it.EQPID
//            }

        }
    }

    var eqpBinding =
        ItemBinding.of<EqpByAreaInfoEntity>(BR.eqpItem, R.layout.item_eqp_by_area_list)
            .bindExtra(BR.onEqpInfoClick,onClick)

    fun getArea(context: Context){
        val map = JsonObject()
        launchOnlyResult({
            val areaList = eqpRepository.getAreaList(map)
            if (areaList.AREALIST.isNotEmpty()){
                for (areaInfoEntity in areaList.AREALIST) {
                    mAreaList.add(areaInfoEntity.AREA!!)
                }
            }else{
                defUI.toastEvent.value = ToastMessageBean(context.getString(R.string.return_area_is_empty),false)
            }
        })
    }
    fun getFabList() {
        val map = JsonObject()
        launchOnlyResult({
            val allFabLinePosition = eqpRepository.getAllFabLinePosition(map)
            fabEntityList.clear()
            fabEntityList.addAll(allFabLinePosition.FABLIST)
            for (areaInfoEntity in allFabLinePosition.FABLIST) {
                fabList.add(areaInfoEntity.FABID+":"+areaInfoEntity.FAB)
            }
        })
    }

    fun getLineList(fab: String) {
        lineEntityList.clear()
        mAreaList.clear()
        fabEntityList.forEach {
            if (it.FAB == fab) {
                lineEntityList.addAll(it.LINELIST)
            }
        }
        lineEntityList.forEach {
            mAreaList.add(it.LINEID+":"+it.LINE)
        }
    }

    @RequiresApi(Build.VERSION_CODES.P)
    fun getEqpByArea(fab:String,area : String){
        val map = JsonObject()
        map.addProperty("FAB",if(fab == "")"" else fab.split(":")[1])
        map.addProperty("FABID",if(fab == "")"" else fab.split(":")[0])
        map.addProperty("AREA",if(area == "")"" else area.split(":")[1])
        map.addProperty("AREAID",if(area == "")"" else area.split(":")[0])
        launchOnlyResult({
            eqpItems.clear()
            val eqpInfoByArea = eqpRepository.getEqpInfoByArea(map)
            eqpItems.addAll(eqpInfoByArea.EQPLIST)
            defUI.callObserve.value = CallObserveBean(0)
//            handler.postDelayed({
//                getEqpByArea(area)
//            },"EQP_BY_AREA",1000*30)
        })


    }

    interface EqpInfoClick{
        fun onEqpInfoItemClick(item : EqpByAreaInfoEntity)
    }

}